<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>if 逻辑判断 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>if 逻辑判断</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 脚本中可以使用 if 逻辑判断，只不过它在 shell 中的语法有点奇怪。</p>
<h2 id="1-不带-else"><a href="#1-不带-else" class="headerlink" title="1.不带 else"></a>1.不带 else</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 判断语句；<span class="keyword">then</span>  </div><div class="line">    <span class="built_in">command</span>  </div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if1.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your scor:"</span> a</div><div class="line"><span class="keyword">if</span>((a-lt60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn' pass the exam."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 if1.sh 中出现了 ((a -lt 60)) 这样的形式，这是 shell 脚本中特有的格式，他等于 if[$a-lt60] ，用一个小括号或者不用都会报错，需要记住这个格式。执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if1.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/01.png?raw=true" alt=""></figure></p>
<h2 id="2-带有-else"><a href="#2-带有-else" class="headerlink" title="2.带有 else"></a>2.带有 else</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span>  判断语句；<span class="keyword">then</span> </div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if2.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your score:"</span> a</div><div class="line"><span class="keyword">if</span>((a&lt;60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn't pass the exam."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good! You passed the exam."</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if2.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;和上例唯一区别的地方是，如果输入大于 60 的数字会有提示。</p>
<h2 id="3-带有-elif"><a href="#3-带有-elif" class="headerlink" title="3.带有 elif"></a>3.带有 elif</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> 判断语句；<span class="keyword">then</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">elif</span> 判断语句二；<span class="keyword">then</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">command</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim if3.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input your score:"</span> a</div><div class="line"><span class="keyword">if</span> ((a&lt;60))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"You didn't pass the exam."</span></div><div class="line"><span class="keyword">elif</span> ((a&gt;=60))&amp;&amp;((a&lt;85))</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"Good! You pass the exam."</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"very good! Your score is very high!"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的 &amp;&amp; 表示 “并且” 的意思，当然也可以使用 || 表示 “或者”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh if3.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/if%20%E9%80%BB%E8%BE%91%E5%88%A4%E6%96%AD/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在判断数值大小除了可以用 (()) 的形式外，还可以用 [] 但是就不能使用  &gt; 、&lt;、= 这样的符号了，要使用 -lt （小于），-gt（大于），-le（小于等于），-ge（大于等于），-eq（等于），-ne（不等于）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面以命令行的形式简单比较</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -lt 5 ];then echo ok;fi</span></div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -gt 5 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -ge 10 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -eq 10 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -ne 10 ];then echo ok;fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再看看 if 中使用 &amp;&amp; 和 || 的情况：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -lt 1 ]||[ $a -gt 5 ];then echo ok;fi </span></div><div class="line">ok</div><div class="line">[root@192 sbin]<span class="comment"># a=10;if [ $a -gt 1 ]||[ $a -lt 10 ];then echo ok;fi </span></div><div class="line">ok</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Shell/9. if 逻辑判断/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
